package test.xml

import groovy.util.slurpersupport.GPathResult

class read_xml {
    public static void main(String[] args) {

        //第一步，创建XmlSlurper类
        def xparser = new XmlSlurper()
        def targetFile = new File("test.xml")
        //轰轰的GPath出场
        GPathResult gpathResult = xparser.parse(targetFile)

        //开始玩test.xml。现在我要访问id=4的book元素。
        //下面这种搞法，gpathResult代表根元素response。通过e1.e2.e3这种
        //格式就能访问到各级子元素....
        def book4 = gpathResult.value.books.book[3]
        println book4
        println 'count: ' + book4.@available
        println "<<" + book4.title + ">>"
        //得到book4的author元素
        def author = book4.author
        println author
        //再来获取元素的属性和textvalue
        assert author.text() == 'Manuel De Cervantes'
    }
}
